using Infrastructure.Attribute;
using Infrastructure.Extensions;
using JTZY.Model.Stock.Dto;
using JTZY.Model.Stock;
using JTZY.Repository;
using JTZY.Service.Stock.IStockService;
using JTZY.Model.Goods;
using JTZY.Model.Order;

namespace JTZY.Service.Stock
{
    /// <summary>
    /// 调仓订单明细表Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(ITransferorderitemService), ServiceLifetime = LifeTime.Transient)]
    public class TransferorderitemService : BaseService<Transferorderitem>, ITransferorderitemService
    {
        /// <summary>
        /// 查询调仓订单明细表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<TransferorderitemDto> GetList(TransferorderitemQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .ToPage<Transferorderitem, TransferorderitemDto>(parm);

            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Transferorderitem GetInfo(long Id)
        {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        public List<Transferorderitem> GetInfo(string orderId, long userId)
        {
            var response = Queryable()
                .LeftJoin<Corecmsuserstock>((it, us) => it.ProductId == us.ProductId && us.UserId == userId)
                .Where(it => it.OrderId == orderId)
                .Select((it, us) => new Transferorderitem
                {
                    OrderId = it.OrderId,
                    Addon =  it.Addon,
                    ImageUrl = !string.IsNullOrEmpty(us.SkuImage) ? us.SkuImage : it.ImageUrl,
                    Nums = it.Nums,
                    Stock = us.Stock??0,
                    ProductId = it.ProductId
                })
                .ToList();

            return response;
        }

        /// <summary>
        /// 添加调仓订单明细表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public Transferorderitem AddTransferorderitem(Transferorderitem model)
        {
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改调仓订单明细表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateTransferorderitem(Transferorderitem model)
        {
            return Update(model, true);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<Transferorderitem> QueryExp(TransferorderitemQueryDto parm)
        {
            var predicate = Expressionable.Create<Transferorderitem>();

            return predicate;
        }
    }
}